package com.cl.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cl.utils.DBUtils;
import com.google.gson.Gson;

/**
 * @author cl
 * @explain Enterprise Login And Sign
 * 
 */
@Controller
public class EnterpriseLASController {

	@RequestMapping("eLogin")
	private String enterpriseLogin(@RequestParam String eName, @RequestParam String ePwd) {
		List<Map<String, Object>> retList = new ArrayList<Map<String, Object>>();
		retList = DBUtils
				.queryForList("select * from user where name= '" + eName + "' and pwd = '" + ePwd + "' and type = 2");
		System.out.println(retList.toString());

		return retList.size() > 0 ? "1" : "0";
	}

	@RequestMapping("/applyEAccountFile")
	private @ResponseBody String applyEnterpriseAccountFile(HttpServletRequest req) {

		// 处理文件
		boolean flag = false;
		String fileName = "";
		String picId = "";
		// 检查输入请求是否为multipart表单数据。
		Properties p = new Properties();
		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
		if (isMultipart) {
			/** 为该请求创建一个DiskFileItemFactory对象，通过它来解析请求。执行解析后，所有的表单项目都保存在一个List中。 **/
			try {
				p.load(this.getClass().getResourceAsStream("/config.properties"));
				DiskFileItemFactory factory = new DiskFileItemFactory();
				factory.setRepository(new File(req.getSession().getServletContext().getRealPath("img/temp")));// 设置缓冲区目录tempPathFile
				ServletFileUpload upload = new ServletFileUpload(factory);
				upload.setHeaderEncoding("UTF-8");// 解决文件乱码问题
				// upload.setSizeMax(sizeMax);// 设置最大文件尺寸
				List<FileItem> items = upload.parseRequest(req);
				// 检查是否符合上传的类型
				// if(!checkFileType(items)) return false;
				Iterator<FileItem> itr = items.iterator();// 所有的表单项
				// 保存文件
				while (itr.hasNext()) {
					FileItem item = (FileItem) itr.next();// 循环获得每个表单项
					if (!item.isFormField()) {// 如果是文件类型
						String name = item.getName();// 获得文件名 包括路径 
						if (name != null) {
							File fullFile = new File(item.getName());
							File savedFile = new File(req.getSession().getServletContext().getRealPath("img"),
									fullFile.getName().trim());
							fileName = fullFile.getName();
							item.write(savedFile);
						}
					}
				}
				flag = true;
			} catch (FileUploadException e) {
				flag = false;
				e.printStackTrace();
			} catch (Exception e) {
				flag = false;
				e.printStackTrace();
			}
		} else {
			flag = false;
			System.out.println("the enctype must be multipart/form-data");
		}
		if (flag) {
			// 保存文件名到表中

		}

		
		 Gson g = new Gson();
		 Map m = new HashMap<String, Object>();
		 m.put("code", 0);
		 m.put("msg", "success");
		 m.put("data", "{  \"src\": \"http://cdn.layui.com/123.jpg\"}");
		  
		return g.toJson(m).toString();
		

	}

	/**
	 * @param companyName
	 *            公司名,legalPerson 法定人,oId one id 为申请者生成唯一的标识编号 设计一个申请表
	 */
	@RequestMapping("applyEAccount")
	private String applyEnterpriseAccount(@RequestParam String companyName, @RequestParam String legalPerson) {
		// 生成个唯一编号
		String oId = UUID.randomUUID().toString().replaceAll("-", "");
		// 处理非文件参数

		// 存表

		return "";

	}

}
